import 'package:scoped_model/scoped_model.dart';
import 'dart:convert';
import '../Common/api.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:typed_data';

class LoadingStatus extends Model {
  bool homeLoadingStatus = false;

  Future<bool> signUp(String phone, String password, String vcode) async {
    Map<String, dynamic> param = {
      'phone': phone,
      'password': password,
      'vcode': vcode,
    };
    print('登录参数' + param.toString());
    final http.Response response =
        await http.post(API.server + API.login, body: (param));
    Map<String, dynamic> jsondata = json.decode(response.body);
   
      print(jsondata);
    if (jsondata['code'] == 1) {
      return true;
    } else {
      return false;
    }
  

    //  print(jsondata);
    //  print(onError);
  }

  Future<Map<String, dynamic>> authToken(String phone, String password) async {
    try {
      Map<String, dynamic> param = {
        'username': phone,
        'password': password,
        'client_id': 'iphone_client_credentials',
        'client_secret': 'spring_security_oauth2.0_iphone_1a2w3e4r',
        'grant_type': 'password',
      };
      print('授权参数' + param.toString());
      final http.Response response =
          await http.post(API.server + API.token, body: (param));
      Map<String, dynamic> jsondata = json.decode(response.body);
      print(jsondata);
      if (jsondata['code'] == 1) {
         final SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString('token', jsondata['access_token']);
    prefs.setString('token', jsondata['refresh_token']);
        return {
          'access_token': jsondata['access_token'],
          'refresh_token': jsondata['refresh_token'],
        };
      } else {
        return null;
      }
    } catch (error) {
      print(error);
      return null;
    }
  }

  Future<Uint8List> imageCode(String phone) async {
    final http.Response response =
        await http.post(API.server + API.imageCode + phone);
    if (response.bodyBytes.length != 0) {
      return response.bodyBytes;
    } else {
      return null;
    }
  }
}
